package com.example.day_day_up.servlet;

import com.example.day_day_up.entity.constants.Constants;
import com.example.day_day_up.entity.vo.ViewMaterialVO;
import com.example.day_day_up.service.MaterialService;
import com.example.day_day_up.utils.JsonUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/material")
public class MaterialServlet extends HttpServlet {
    private MaterialService materialService;

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext servletContext = getServletContext();
        materialService = (MaterialService) servletContext.getAttribute("materialService");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        ViewMaterialVO materialDTO = materialService.selectMaterialByID(id);
        HttpSession session = req.getSession();
        session.setAttribute(Constants.MATERIAL_KEY + id, materialDTO);
        JsonUtils.writeSuccessResponse(resp, materialDTO);
    }
}
